#include "waterwavewidget.h"

WaterWaveWidget::WaterWaveWidget(QWidget *parent) : BHXJGLBaseWidget(parent)
{
    QSurfaceFormat surfaceFormat(this->format());
    surfaceFormat.setSamples(8);
    setFormat(surfaceFormat);

}
WaterWaveWidget::~WaterWaveWidget()
{

    delete wave;
}

void WaterWaveWidget::initializeGL()
{
    initializeOpenGLFunctions();

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    shader.Init(this,":/regvertex.vert",":/regfragment.frag");


    camera.SetPosition(glm::vec3(0.0f, 0.0f, 6.0f));
    camera.PitchLimit();

    projection    = glm::mat4(1.0f);
    float width=static_cast<float>(this->width());
    float height=static_cast<float>(this->height());
    height=height>0?height:1;
    projection = glm::perspective(glm::radians(45.0f), width / height, 0.05f, 100.0f);


    wave=new BHXJGLWaterWave();
    wave->Init(this);
}
void WaterWaveWidget::paintGL()
{
    using namespace glm;

    glEnable(GL_STENCIL_TEST);
    glEnable(GL_DEPTH_TEST);
    //glStencilMask(0xFF);//
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
    //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);


    if(_PaintLine)
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    else
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    BHXJGLPaintParam gpp;
    gpp.setProjection(projection);
    gpp.setView(camera.View(true));

    gpp.setSize(2,2,1);

    gpp.setUp(vec3(0,0,-1));
    gpp.setRight(vec3(1,0,0));
    gpp.setPosition(vec3(0,0,1),true);
    wave->setviewPos(camera.GetPosition());
    wave->sett(t);
    wave->Paint(gpp);




    //    for(int i=0;i<10;++i)
    //    {
    //        gpp.setPosition(vec3(3.5+1.5*i,0,1),true);
    //       tachometer->setviewPos(camera.GetPosition());
    //       tachometer->setRpm(this->_RPM);
    //       tachometer->Paint(gpp);
    //    }
}


void WaterWaveWidget::resizeGL(int width, int height)
{
    projection    = glm::mat4(1.0f);
    height=height>0?height:1;
    projection = glm::perspective(glm::radians(45.0f), static_cast<float>(width) / static_cast<float>(height), 0.1f, 100.0f);
    // projection = glm::ortho(-(float)width / (float)height, (float)width / (float)height,-1.f, 1.f, 0.1f, 100.0f);
}

void WaterWaveWidget::timerEvent(QTimerEvent *e)
{
    BHXJGLBaseWidget::timerEvent(e);
    t+=0.01f;

    updated=true;
}

